package my.assistant.util;

import my.assistant.R;
import my.assistant.reminder.RemindVO;
import android.content.res.Resources;

public final class ReminderUtils
{

	public static RemindVO buildReminderVO(int n)
	{
		RemindVO remindVO = new RemindVO();
		remindVO.enableBsWeek = ResUtils.getBooleanRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_BSWEEK);
		remindVO.enableVibrate = ResUtils.getBooleanRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_ENABLE_VIBRATE);
		remindVO.enable = ResUtils.getBooleanRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_ENABLE);
		remindVO.hour = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_HOUR);
		remindVO.minute = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_MINUTE);
		remindVO.sound = ResUtils.getStringRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_SOUND);
		String repeat = ResUtils.getStringRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_REPEAT);
		String[] arr = repeat.split(Constants.COMA);
		for (int i = 0; i < arr.length; i++)
		{
			remindVO.repeatDay[i] = Boolean.valueOf(arr[i]);
		}
		return remindVO;
	}

	public static boolean checkReminderEnable(String id)
	{
		return ResUtils.getBooleanRes(ResKey.REMINDVO_PREFIX + id + ResKey.REMINDVO_ENABLE);
	}

	public static void setReminderEnable(String id, boolean enable)
	{
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + id + ResKey.REMINDVO_ENABLE, enable);
	}

	public static String buildRemindStr(StringBuilder buff, Resources resources, int n)
	{
		buff.setLength(0);
		boolean bsWeek = ResUtils.getBooleanRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_BSWEEK);
		String hour = ResUtils.getStringRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_HOUR);
		String minute = ResUtils.getStringRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_MINUTE);
		String repeat = ResUtils.getStringRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_REPEAT);
		buff.append(hour).append(Constants.COLON);
		buff.append(minute.length() == 2 ? minute : "0" + minute);
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		String[] arr = repeat.split(Constants.COMA);
		String[] days = resources.getStringArray(R.array.select_week_n);
		for (int i = 0; i < arr.length; i++)
		{
			if (Boolean.valueOf(arr[i]))
			{
				buff.append(Constants.BLANK);
				buff.append(days[i]);
			}
		}
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		buff.append(Constants.LEFT);
		if (bsWeek)
		{
			buff.append(resources.getString(R.string.enable_y));
		} else
		{
			buff.append(resources.getString(R.string.enable_n));
		}
		buff.append(Constants.RIGHT);
		return buff.toString();
	}

	public static String buildRemindStr(StringBuilder buff, Resources resources, RemindVO reVO)
	{
		buff.setLength(0);
		buff.append(reVO.hour).append(Constants.COLON);
		buff.append(reVO.minute > 9 ? reVO.minute : "0" + reVO.minute);
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		String[] days = resources.getStringArray(R.array.select_week_n);
		for (int i = 0; i < reVO.repeatDay.length; i++)
		{
			if (Boolean.valueOf(reVO.repeatDay[i]))
			{
				buff.append(Constants.BLANK);
				buff.append(days[i]);
			}
		}
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		buff.append(Constants.LEFT);
		if (reVO.enableBsWeek) // big small week
		{
			buff.append(resources.getString(R.string.enable_y));
		} else
		{
			buff.append(resources.getString(R.string.enable_n));
		}
		buff.append(Constants.RIGHT);
		return buff.toString();
	}

	public static int saveNewRemindVO(RemindVO vo)
	{
		int n = 0;
		while (ResUtils.hasKeyPrefix(ResKey.REMINDVO_PREFIX + n))
		{
			n++;
		}
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_HOUR, vo.hour, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_MINUTE, vo.minute, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_BSWEEK, vo.enableBsWeek, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_REPEAT, vo.repeatDay);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_SOUND, vo.sound);
		return n;
	}

	public static String getTimeItem(StringBuilder buff, Resources resources, RemindVO remindVO)
	{
		buff.setLength(0);
		buff.append(resources.getString(R.string.reminder_time));
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		buff.append(remindVO.hour);
		buff.append(Constants.COLON);
		buff.append(remindVO.minute > 9 ? remindVO.minute : Constants.ZERO + remindVO.minute);
		return buff.toString();
	}

	public static String getSoundItem(StringBuilder buff, Resources resources, RemindVO remindVO)
	{
		buff.setLength(0);
		buff.append(resources.getString(R.string.reminder_sound));
		if (remindVO.sound != null)
		{
			if (remindVO.sound.length() == 1)
			{
				buff.append(resources.getStringArray(R.array.default_sound)[Integer.valueOf(remindVO.sound)]);
			}
		}
		return buff.toString();
	}

	public static String getRepeatDayItem(StringBuilder buff, Resources resources, RemindVO remindVO,
	        boolean[] tmpRepeatDay, String[] dayStrArr, boolean edit)
	{
		buff.setLength(0);
		buff.append(resources.getString(R.string.reminder_repeat));
		buff.append(Constants.BLANK);
		buff.append(Constants.BLANK);
		boolean has = false;
		for (int i = 0; i < tmpRepeatDay.length; i++)
		{
			if (edit)
			{
				tmpRepeatDay[i] = remindVO.repeatDay[i];
			} else
			{
				if (remindVO != null)
				{
					remindVO.repeatDay[i] = tmpRepeatDay[i];
				}
			}
			if (tmpRepeatDay[i])
			{
				if (has)
				{
					buff.append(Constants.BLANK);
				} else
				{
					has = true;
				}
				buff.append(dayStrArr[i]);
			}
		}
		return buff.toString();
	}

	public static void delReminder(int position)
	{
		ResUtils.delRes(ResKey.REMINDVO_PREFIX + position);
	}

	public static String getBsWeekEnableItem(StringBuilder buff, Resources resources, RemindVO remindVO)
	{
		buff.setLength(0);
		String arr = resources.getString(R.string.reminder_bsweek_enable);
		buff.append(arr);
		buff.append(Constants.COLON);
		buff.append(Constants.BLANK);
		if (remindVO.enableBsWeek)
		{
			buff.append(resources.getString(R.string.enable_y));
		} else
		{
			buff.append(resources.getString(R.string.enable_n));
		}
		return buff.toString();
	}

	public static String getVibrateEnableItem(StringBuilder buff, Resources resources, RemindVO remindVO)
	{
		buff.setLength(0);
		String arr = resources.getString(R.string.reminder_vibrate_enable);
		buff.append(arr);
		buff.append(Constants.COLON);
		buff.append(Constants.BLANK);
		if (remindVO.enableVibrate)
		{
			buff.append(resources.getString(R.string.enable_y));
		} else
		{
			buff.append(resources.getString(R.string.enable_n));
		}
		return buff.toString();
	}

	public static void updateNewRemindVO(RemindVO vo, String n)
	{
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_HOUR, vo.hour, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_MINUTE, vo.minute, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_BSWEEK, vo.enableBsWeek, false);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_REPEAT, vo.repeatDay);
		ResUtils.setRes(ResKey.REMINDVO_PREFIX + n + ResKey.REMINDVO_SOUND, vo.sound);
	}

	public static boolean checkReminderExist(String editIndex, int hourOfDay, int minute)
	{
		if ("-1".equals(editIndex))
		{
			// new
			int i = 0;
			while (i < Constants.MAX_REMINDER_COUNT)
			{
				int ehour = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + i + ResKey.REMINDVO_HOUR);
				int eminute = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + i + ResKey.REMINDVO_MINUTE);
				if (ehour == hourOfDay && eminute == minute)
				{
					return true;
				}
				i++;
			}
		} else
		{
			int ehour = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + editIndex + ResKey.REMINDVO_HOUR);
			int eminute = ResUtils.getIntRes(ResKey.REMINDVO_PREFIX + editIndex + ResKey.REMINDVO_MINUTE);
			return (ehour == hourOfDay && eminute == minute);
		}
		return false;
	}
}
